<template>
  <el-dialog :title="type === 'add' ? '新增用户' : '编辑用户'"
    :visible="dialogVisible"
    width="650px"
    @close="close">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-row>
        <el-col :span="12">
          <el-form-item label="用户名称" prop="username">
            <el-input v-model="ruleForm.username"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="用户昵称" prop="nickname">
            <el-input v-model="ruleForm.nickname"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="手机号码" prop="phone">
            <el-input v-model="ruleForm.phone"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="ruleForm.email"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="用户性别" prop="gender">
            <el-select v-model="ruleForm.gender" placeholder="请选择">
              <el-option
                v-for="item in genderOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="角色" prop="role">
            <el-select v-model="ruleForm.role" placeholder="请选择">
              <el-option
                v-for="item in roleOptions"
                :key="item.id"
                :label="item.remark"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="ruleForm.status">
              <el-radio :label="true">启用</el-radio>
              <el-radio :label="false">停用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input type="textarea" v-model="ruleForm.remark"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="reset('ruleForm')">取 消</el-button>
      <el-button v-if="type === 'add'" type="primary" @click="save('ruleForm')" :loading="loading">确 定</el-button>
      <el-button v-if="type === 'edit'" type="primary" @click="update('ruleForm')" :loading="loading">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      ruleForm: {
        username: '',
        phone: '',
        gender: '',
        remark: '',
        email: '',
        status: true,
        nickname: '',
        role: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名称', trigger: 'blur' }
        ],
        role: [
          { required: true, message: '请输入', trigger: 'change' }
        ],
      },
      loading: false,
      type: 'add',
      genderOptions: [
        { label: '女', value: '女' },
        { label: '男', value: '男' },
        { label: '未知', value: '未知' }
      ],
      roleOptions: []
    }
  },
  props: {
    dialogVisible: {
      type: Boolean
    },
    list: {
      type: Array
    }
  },
  methods: {
    close () {
      this.$refs.ruleForm.resetFields();
      this.$emit('update:dialogVisible', false)
    },
    open () {
      this.type = 'add'
      this.$emit('update:dialogVisible', true)
      this.getRoleList()
    },
    save(formName) { // 新增
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          const params = {
            user: {
              username: this.ruleForm.username,
              phone: this.ruleForm.phone,
              gender: this.ruleForm.gender,
              remark: this.ruleForm.remark,
              email: this.ruleForm.email,
              status: this.ruleForm.status,
              nickname: this.ruleForm.nickname,
            },
            role: this.ruleForm.role
          }
          this.$API({
            name: 'addUser',
            data: params,
            requireAuth: true
          }).then(res => {
            if (res.data.code === 200) {
              this.$message.success(res.data.msg)
              this.close()
              this.$emit('getAll')
            } else {
              this.$message.error(res.data.msg)
            }
          }).catch(error => {
            this.$message.error(error)
          }).finally(() => {
            this.loading = false
          })
        }
      })
    },
    reset(formName) {
      this.$refs[formName].resetFields();
      this.$emit('update:dialogVisible', false)
    },
    detaliInfo (id) { // 根据id获取详情
      this.getRoleList()
      this.type = 'edit'
      this.$emit('update:dialogVisible', true)
      this.$API({
        name: 'detailUser',
        paths: [id],
        requireAuth: true
      }).then (res => {
        if (res.data.code === 200) {
          if (res.data.data.ur[0].name === 'user') {
            this.$nextTick(() => {
              let arr = []
              this.roleOptions.forEach(item => {
                if (item.name === 'user') {
                  arr.push(item)
                }
              })
              this.roleOptions = arr
            })
          }
          this.ruleForm = res.data.data // 后期多角色需要修改
          this.ruleForm.role = res.data.data.ur[0].id
        } else {
          this.$message.error(res.data.msg)
        }
      }).catch(error => {
        this.$message.error(error)
      })
    },
    update(formName) { // 编辑
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          const params = {
            user: {
              id: this.ruleForm.id,
              username: this.ruleForm.username,
              phone: this.ruleForm.phone,
              gender: this.ruleForm.gender,
              remark: this.ruleForm.remark,
              email: this.ruleForm.email,
              status: this.ruleForm.status,
              nickname: this.ruleForm.nickname,
            },
            roleIds: this.ruleForm.role
          }
          this.$API({
            name: 'updateUser',
            data: params,
            requireAuth: true
          }).then(res => {
            if (res.data.code === 200) {
              this.$message.success(res.data.msg)
              this.close()
              this.$emit('getAll')
            } else {
              this.$message.error(res.data.msg)
            }
          }).catch(error => {
            this.$message.error(error)
          }).finally(() => {
            this.loading = false
          })
        }
      })
    },
    getRoleList () { // 获取角色列表
      this.$API({
        name: 'getRoleList',
        params: {
          page: 1,
          size: 10
        },
        requireAuth: true
      }).then (res => {
        if (res.data.code === 200) {
          // 此时判断当前登录用户的角色，普通用户只有普通用户选项，管理员
          if (this.type === 'add') {
            let arr = []
            res.data.data.data.forEach(item => {
              if (item.name === 'user') {
                arr.push(item)
              }
            })
            this.roleOptions = arr
          } else {
            this.roleOptions = res.data.data.data
          }
        } else {
          this.$message.error(res.data.msg)
        }
      }).catch(error => {
        this.$message.error(error)
      }).finally(() => {
      })
    }
  }
}
</script>

<style>

</style>